package com.bdqn.t382.controller;

import com.bdqn.t382.pojo.HotSpotConsultation;
import com.bdqn.t382.pojo.Informations;
import com.bdqn.t382.pojo.Page;
import com.bdqn.t382.pojo.Replies;
import com.bdqn.t382.service.HotSpotConsultationService;
import com.bdqn.t382.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@ResponseBody
@RequestMapping("/hotSpotConsultation")
public class HotSpotConsultationController {
    @Autowired
    HotSpotConsultationService hotSpotConsultationService;

    @RequestMapping("/page")
    public R page(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                  @RequestParam(value = "pageSize", defaultValue = "3") Integer pageSize) {
        Page page = hotSpotConsultationService.getPage(pageNo, pageSize);
        if (page.getList() == null || page.getList().size() == 0) {
            return R.fail();
        } else {
            return R.ok(page);

        }
    }

    @RequestMapping("/content")
    public R content(@RequestParam Integer id) {
        List<HotSpotConsultation> hotSpotConsultation = hotSpotConsultationService.getHotSpotConsultation(id);
        if (hotSpotConsultation == null || hotSpotConsultation.size() < 0) {
            return R.fail();
        } else {
            return R.ok(hotSpotConsultation);
        }
    }

    @RequestMapping("/add")
    public R add(@RequestBody Replies replies) {
        int num = hotSpotConsultationService.insertReplies(replies);
        if (num < 0) {
            return R.fail();
        } else {
            return R.ok(replies);
        }
    }

    @RequestMapping("/update")
    public R update(@RequestBody Informations informations) {
        int num = hotSpotConsultationService.updateInformations(informations);
        if (num < 0) {
            return R.fail();
        } else {
            return R.ok(informations);
        }
    }

    @RequestMapping("/delete")
    public R delete(@RequestParam Integer id) {
        //删除Informations对应数据
        int num1 = hotSpotConsultationService.deleteInformations(id);
        //删除Replies对应数据
        int num2 = hotSpotConsultationService.deleteReplies(id);
        if (num1 < 0 || num2 < 0) {
            return R.fail();
        } else {
            return R.ok(num1);
        }
    }
}
